package com.vandagroup.portal.role;

/**
 * @author po tools
 *  
 * @date Feb.9,2004 
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.vandagroup.common.bo.UserContext;
import com.vandagroup.common.webapp.ActiveAction;
import com.vandagroup.portal.PortalParas;
import com.vandagroup.portal.menu.treeview.CheckboxTreeView;

public class RoleAction extends ActiveAction {

	/**
	 * 調度
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {

		if (task == null || task.equals("")) {
			return (forwardErrorPage(request, mapping, "操作任務參數不存在"));
		}
		if (step == null || step.equals("")) {
			return (forwardErrorPage(request, mapping, "操作步驟參數不存在"));
		}
		try {
			if (task.equals("list")) {
				return doViewList(mapping, form, request, response, user);
			} else if (task.equals("add")) {
				return doAdd(mapping, form, request, response, user);
			} else if (task.equals("edit")) {
				return doEdit(mapping, form, request, response, user);
			} else if (task.equals("view")) {
				return doView(mapping, form, request, response, user);
			} else if (task.equals("delete")) {
				return doDelete(mapping, form, request, response, user);
			} else if (task.equals("distributeFunc")) {
				return doDistributeFunc(mapping, form, request, response, user);
			} else {
				return (forwardErrorPage(request, mapping, "操作類型參數不存在"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (forwardErrorPage(request, mapping, "角色資訊維護錯誤", e));
		}
	}

	/**
	 * 添加角色信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return ActionForward
	 * @throws Exception
	 *             Create Date : 2004-2-9
	 */
	public ActionForward doAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		try {
			CheckboxTreeView treeView = new CheckboxTreeView(user.getOrgName(),
					"ORG", "ORGID", "ORGNAME", "UPORGID", user.getOrgID(),
					"orgID", "STATUS <> '" + PortalParas.STATUS_DELETE + "'");
			String contexPath = request.getContextPath().equals("/") ? ""
					: request.getContextPath();
			treeView.setContexPath(contexPath);
			treeView.create();
			request.setAttribute("treeView", treeView);

			if (step.equals("init")) {
				return (mapping.findForward("add"));
			} else if (step.equals("submit")) {
				RoleBO bo = new RoleBO();
				RoleFormBean bean = (RoleFormBean) form;
				int rtn = bo.add(bean, user);
				request.setAttribute("success", "1");
				return (mapping.findForward("add"));
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return (forwardErrorPage(request, mapping, "添加角色資訊時錯誤", e));
		}
	}

	/**
	 * 查看角色信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return ActionForward
	 * @throws Exception
	 *             Create Date : 2004-2-9
	 */
	public ActionForward doView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		try {
			RoleFormBean bean = (RoleFormBean) form;
			RoleBO bo = new RoleBO();

			CheckboxTreeView treeView = new CheckboxTreeView(user.getOrgName(),
					"ORG", "ORGID", "ORGNAME", "UPORGID", user.getOrgID(),
					"orgID", null, bo.retrieveOrgs(bean.getRoleID()));
			treeView.setDisabled(true);
			String contexPath = request.getContextPath().equals("/") ? ""
					: request.getContextPath();
			treeView.setContexPath(contexPath);
			treeView.create();
			request.setAttribute("treeView", treeView);

			if (step.equals("init")) {
				try {
					request.setAttribute("roleStatus", bo.getRoleStatus(user
							.getOrgID(), bean.getRoleID()));
					request.setAttribute("form", bo.retrieveForm(bean));
					return (mapping.findForward("view"));
				} catch (Exception e) {
					e.printStackTrace();
					return (forwardErrorPage(request, mapping, "查看角色資訊時錯誤", e));
				}

			} else if (step.equals("submit")) {
				try {
					int rows = bo.updateRoleStatus(bean, user.getOrgID());
					return (forward("/Role.do?task=view&step=init&roleID="
							+ bean.getRoleID()));
				} catch (Exception e) {
					e.printStackTrace();
					return (forwardErrorPage(request, mapping, "查看角色資訊時錯誤", e));
				}
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return (forwardErrorPage(request, mapping, "查看角色資訊時錯誤", e));
		}
	}

	/**
	 * 修改角色資訊
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return ActionForward
	 * @throws Exception
	 *             Create Date : 2004-2-9
	 */
	public ActionForward doEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		try {
			RoleFormBean bean = (RoleFormBean) form;
			RoleBO bo = new RoleBO();

			CheckboxTreeView treeView = new CheckboxTreeView(user.getOrgName(),
					"ORG", "ORGID", "ORGNAME", "UPORGID", user.getOrgID(),
					"orgID", "STATUS <> '" + PortalParas.STATUS_DELETE + "'",
					bo.retrieveOrgs(bean.getRoleID()));
			String contexPath = request.getContextPath().equals("/") ? ""
					: request.getContextPath();
			treeView.setContexPath(contexPath);

			treeView.create();
			request.setAttribute("treeView", treeView);

			if (step.equals("init")) {
				try {
					request.setAttribute("form", bo.retrieveForm(bean));
					return (mapping.findForward("edit"));
				} catch (Exception e) {
					e.printStackTrace();
					return (forwardErrorPage(request, mapping, "編輯查看角色資訊時錯誤", e));
				}

			} else if (step.equals("submit")) {
				try {
					bo.update(bean, user);
					request.setAttribute("success", "1");
					return (forward("/Role.do?task=edit&step=init&roleID="
							+ bean.getRoleID()));
				} catch (Exception e) {
					e.printStackTrace();
					return (forwardErrorPage(request, mapping, "編輯角色資訊時錯誤", e));
				}
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return (forwardErrorPage(request, mapping, "編輯角色資訊時錯誤", e));
		}
	}

	/**
	 * 查看列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return ActionForward
	 * @throws Exception
	 *             Create Date : 2004-2-9
	 */
	public ActionForward doViewList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		if (step.equals("list")) {
			return (mapping.findForward("list"));
		} else if (step.equals("innerList")) {

			String strPageNO = (String) request.getParameter("pageNO");
			int pageNO = strPageNO == null ? Integer.parseInt("1") : Integer
					.parseInt(strPageNO);
			try {
				RoleBO bo = new RoleBO();
				RoleFormBean bean = (RoleFormBean) form;
				com.vandagroup.common.util.Result t = bo.query(pageNO, bean,
						user);
				request.setAttribute("result", t);
				request.setAttribute("form", bean);
				return (forward(mapping.findForward("innerList")));
			} catch (Exception e) {
				e.printStackTrace();
				return (forwardErrorPage(request, mapping, "查詢角色資訊時錯誤", e));
			}

		} else {
			return (forwardErrorPage(request, mapping, "操作步驟不正確"));
		}
	}

	/**
	 * 刪除角色資訊
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return ActionForward
	 * @throws Exception
	 *             Create Date : 2004-2-9
	 */
	public ActionForward doDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext user) throws Exception {
		try {
			CheckboxTreeView treeView = new CheckboxTreeView(user.getOrgName(),
					"ORG", "ORGID", "ORGNAME", "UPORGID", user.getOrgID(),
					"orgID", null);
			String contexPath = request.getContextPath().equals("/") ? ""
					: request.getContextPath();
			treeView.setContexPath(contexPath);
			treeView.create();
			request.setAttribute("treeView", treeView);

			RoleFormBean bean = (RoleFormBean) form;
			RoleBO bo = new RoleBO();
			int rtn = bo.remove(bean);
			request.setAttribute("success", "1");
			return (mapping.findForward("add"));
		} catch (Exception e) {
			e.printStackTrace();
			return (forwardErrorPage(request, mapping, "刪除角色資訊時錯誤", e));
		}

	}

	/**
	 * 為角色分配許可權
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return ActionForward
	 * @throws Exception
	 *             Create Date : 2004-2-10
	 */
	public ActionForward doDistributeFunc(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext user) throws Exception {
		try {
			RoleFormBean bean = (RoleFormBean) form;
			RoleBO bo = new RoleBO();
			if (step.equals("init")) {
				return forward(mapping.findForward("disMng"));
			} else if (step.equals("roleList")) {
				String strPageNO = (String) request.getParameter("pageNO");
				int pageNO = strPageNO == null ? Integer.parseInt("1")
						: Integer.parseInt(strPageNO);
				try {
					com.vandagroup.common.util.Result t = bo.query(pageNO,
							bean, user);
					request.setAttribute("result", t);
					request.setAttribute("form", bean);
					return (forward(mapping.findForward("disRoleList")));
				} catch (Exception e) {
					e.printStackTrace();
					return (forwardErrorPage(request, mapping, "查詢角色資訊時錯誤", e));
				}
			} else if (step.equals("funcList")) {

				CheckboxTreeView treeView = new CheckboxTreeView("功能列表",
						"FUNC", "FUNCID", "FUNCNAME", "PARENTFUNCID",
						"$$$$$$$$", "funcID",
						"FUNCID IN ( SELECT FUNCID FROM ORGFUNC WHERE ORGID = '"
								+ user.getOrgID() + "' )", bo.getFuncIDs(user
								.getOrgID(), bean.getRoleID()));
				String contexPath = request.getContextPath().equals("/") ? ""
						: request.getContextPath();
				treeView.setContexPath(contexPath);
				treeView.create();
				request.setAttribute("treeView", treeView);
				request.setAttribute("roleID", bean.getRoleID());
				return forward(mapping.findForward("disFuncList"));
			} else if (step.equals("submit")) {
				bo.disFunc(bean, user);
				System.out.println("bean.getFuncID().length:"
						+ bean.getFuncID().length);
				return forward("/Role.do?task=distributeFunc&step=funcList&roleID="
						+ bean.getRoleID());
			} else {
				return forwardErrorPage(request, mapping, "操作步驟參數值不存在！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return forwardErrorPage(request, mapping, "為角色分配許可權時發生錯誤！", e);
		}
	}

}
